﻿using CommonHelper;
using CommonHelper.AutofacCommon;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Order.MicroService.Domain;
using Order.MicroService.IRepository;
using Order.MicroService.IService;
using Order.MicroService.Models;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;

namespace Order.MicroService.Api.Controllers
{
    /// <summary>
    /// 测试接口
    /// </summary>
    [Route("api/[controller]")]
    [ApiController]
    public class TestServiceController : ControllerBase
    {
        private readonly ITestService _testService;


        private readonly MyDbContext _myDbContext;

        public TestServiceController(ITestService testService, MyDbContext myDbContext)
        {
            Console.WriteLine($"{this.GetType().Name} 被构造");
             _testService = testService;
            _myDbContext = myDbContext;
        }

        /// <summary>
        /// 测试接口TestServiceA
        /// </summary>
        /// <param name="name">姓名</param>
        /// <returns></returns>
        [HttpGet("TestServiceA")]
        public string TestServiceA([Required(ErrorMessage ="姓名不能为空")] string name)
        {
            return _testService.GetTestA();
        }
        
        /// <summary>
        /// 获取Orders文档
        /// </summary>
        /// <returns></returns>
        [HttpGet("GetOrders")]
        public ApiResult<Orders> GetOrders()
        {
            Orders orders = new Orders();
            //Orders orders = _myDbContext.Orders.FromSqlRaw("select * from Orders where id=6").ToList().FirstOrDefault();
            //return ApiResult<Orders>.SuccessResult(_orderRepository.GetOrders());
            var test = _myDbContext.Set<Orders>().Where(x => x.Id == 5).ToList();
            var nicai = _myDbContext.Set<Orders>().AsEnumerable().Where(x => x.Id == 6);
            
            return ApiResult<Orders>.SuccessResult(orders);
        }

        [HttpGet("TestList")]
        public Hashtable TestList()
        {
            ArrayList list = new ArrayList();
            list.Add(1);
            //测试List<T>
            List<string> listNew = new List<string>();
            listNew.Add("一号元素");
            listNew.Add("二号元素");
            listNew.Add("三号元素");
            listNew.Add("四号元素");
            listNew.Add("五号元素");

            string[] str = new string[] { "六号元素", "七号元素", "八号元素" };
            string nicai = "woaini";
            listNew.Add(nicai);
            listNew.AddRange(str);

            var count = str.Length;
            var count1 = listNew.Count;
            var count2 = listNew.Capacity;
            var count3 = listNew.IndexOf("woaini");

            listNew.Reverse();
            string[] str1 = listNew.ToArray();

            Hashtable hashtable = new Hashtable();
            hashtable.Add("woaini", "shabi");
            hashtable.Add("name", 2);
            

            var hash1 = hashtable["woaini"];
            var hash2 = hashtable.Keys;
            var hash3 = hashtable.Values;

            var hash6 = hash2.ToString()[0];
            foreach (var item in hash2)
            {
                Console.WriteLine(item.ToString());
            }

            var hash7 = hash2.ToString()[1];
            var hash4 = hash2.Count;
            var hash5 = hash2.GetHashCode();
           
            foreach (DictionaryEntry item in hashtable)
            {
                Console.WriteLine("Key = {0}, Value = {1}", item.Key, item.Value);
            }


            Dictionary<int, string> keyValues = new Dictionary<int, string>();
            keyValues.Add(2, "woaini");
            keyValues.Add(3, "shabi");


            var key1 = keyValues[2].ToString();

            foreach (KeyValuePair<int,string> item in keyValues)
            {
                Console.WriteLine($"{item.Key}\t{item.Value}\t over");
            }

            // 声明
            List<string> listA = new List<string>() { "hello", " ", "wrold" };
            // 循环
            var i = 0;
            listA.ForEach(item =>
            {
                Console.WriteLine($"第{i + 1}个");
                Console.WriteLine(item);
                i++;
            });



            Dictionary<int, int> dic = new Dictionary<int, int>();

            dic.Add(0, 0);
            dic.Add(1, 1);
            dic.Add(2, 2);
            Console.WriteLine("仅仅经过添加元素处理之后：");
            foreach (KeyValuePair<int, int> kvp in dic)
            {
                Console.WriteLine("Key:" + kvp.Key + " Value:" + kvp.Value);
            }

            dic.Remove(0);
            dic.Add(3, 3);
            Console.WriteLine("经过删除和添加元素处理之后：");
            foreach (KeyValuePair<int, int> kvp in dic)
            {
                Console.WriteLine("Key:" + kvp.Key + " Value:" + kvp.Value);
            }




            return hashtable;
        }
    }
}
